package cate.game.activity.limitback;

import cate.common.table.activity.limitback.row.LimitBackRow;
import cate.common.table.item.hero.row.HeroBaseRow;
import cate.common.util.GameResult;
import cate.game.activity.base.core.RoleActivitySingle;
import cate.game.activity.limitback.msg.LimitBackInfoResp;
import cate.game.notice.msg.NoticeDialogResp;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import cate.game.res.RewardShow;
import cate.game.role.Role;
import cate.game.role.bag.hero.Hero;
import cate.game.train.hero.msg.HeroBackResp;
import easy.java.practice.errorcode.EcResult;


public class LimitBackActivity extends RoleActivitySingle {
    @Override
    public void onRoleOpen() {

    }

    @Override
    public void onRoleEnd() {

    }

    @Override
    public void initSend() {
        noticeUpdate();
    }

    @Override
    public void excelCheck() {

    }

    private GameResult<Hero> findHeroResult(String uid) {
        GameResult<Hero> r = new GameResult<>();
        Hero hero = role.getBag().hero.getItem(uid);
        if (hero == null) {
            return r.fail("英雄不存在！");
        }
        r.data = hero;
        return r.success();
    }

    /**
     * 英雄回退
     */
    public void back(Role role, String uid) {
        if (!enable()) {
            role.getGame().notice.message(role, "活动未开启！");
            return;
        }
        EcResult<Hero> r = findHeroResult(uid);
        if (!r.ok()) {
            role.getGame().notice.message(role, r.message);
            return;
        }
        Hero hero = r.data;
        EcResult<Void> offRes = role.getGame().train.hero.fastOffHero(role, uid);
        if (!offRes.ok()) {
            return;
        }
        int heroStar = hero.getTpl(role.getGame()).star;
        if (heroStar < 6 || heroStar > 9) {
            role.getGame().notice.message(role, "该英雄不可回退");
            return;
        }
        if (hero.lock.islocked()) {
            role.getGame().notice.message(role, "该英雄已被锁定");
            return;
        }
        int backStar = 5;
        HeroBaseRow heroRow = hero.getTpl(role.getGame());
        HeroBaseRow backHeroRow = role.getGame().table.hero.base.getRow(heroRow.protoId, backStar);
        if (backHeroRow == null) {
            role.getGame().notice.message(role, "回退英雄不存在");
            return;
        }
        LimitBackRow consumeRow = role.getGame().table.limitBack.limitBack.get(heroStar);
        if (consumeRow == null) {
            role.getGame().notice.message(role, "回退方式有误");
            return;
        }
        EcResult<MixResItem> consumeRes = new MixRes(consumeRow.consumeStr).consume(role, null);
        if (!consumeRes.ok()) {
            role.getGame().notice.tipResItem(role, consumeRes.data);
            role.getGame().notice.message(role, consumeRes.message);
            return;
        }
        RewardShow show = role.getGame().train.hero.backExe(role, hero, backStar, 1, 0);
        if (!show.isEmpty()) {
            role.getGame().notice.dialog(role, NoticeDialogResp.TYPE_REWARD, "恭喜获得", 0, show.tplRwd, show.insRwd);
        }
        role.sendNow(new HeroBackResp(uid, hero, show));
        role.getBag().hero.markHeroChanged();
    }


    public void noticeUpdate() {
        role.sendNow(new LimitBackInfoResp(this));
    }
}
